// pages/verify/verify.js
let db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    admin:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      admin:wx.getStorageSync('admin')
    })
  },
  xinjian(e){
    let {index}=e.currentTarget.dataset
    let getDetail = this.data.admin[index]
    let that = this
    wx.showModal({
      cancelText: '取消',
      confirmText: '确定',
      content: '确定要新建吗？',
      showCancel: true,
      success: (result) => {
        if(result.confirm){
          wx.showLoading({
            title: '请稍等...',
          })
          db.collection('moneyList').add({
            data:{
              type:new Date().getTime(),
              icon:getDetail.icon,
              zanzhu:getDetail.zanzhu,
              jiangpin:getDetail.jiangpin,
              quan:getDetail.quan,
              count:getDetail.count,
              user:[],
              zhuli:getDetail.zhuli,
              zhongjiang:[]
            }
          })
          .then(e=>{
            wx.hideLoading({
              success: (res) => {},
            })
            wx.showToast({
              title: '新建成功',
              duration:1000
            })
          })
          .catch(e=>{
            wx.hideLoading({
              success: (res) => {},
            })
            wx.showToast({
              title: '新建失败',
              icon:none,
              duration:1000
            })
          })
        }
      }
    })
  },
  del(e){
    let {index} = e.currentTarget.dataset
    let type = this.data.admin[index].type
    let that = this
    let admin = that.data.admin
    wx.showModal({
      cancelText: '取消',
      confirmText: '确定',
      content: '确定要删除吗？',
      showCancel: true,
      success: (result) => {
        if(result.confirm){
          wx.showLoading({
            title: '请稍等...',
          })
          db.collection('moneyList').where({
            type
          }).remove()
          .then(e=>{
            wx.hideLoading({
              success: (res) => {},
            })
            wx.showToast({
              title: '删除成功',
              duration:1000
            })
          })
          .catch(e=>{
            wx.hideLoading({
              success: (res) => {},
            })
            wx.showToast({
              title: '删除失败',
              icon:none,
              duration:1000
            })
          })
          db.collection('admin').where({
            type
          }).remove()
          admin.splice(index,1)
          that.setData({
            admin
          })
        }
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})